<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/30
 * Time: 14:56
 */

namespace app\shop\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use think\Image;
use think\Response;

class Upload extends Controller
{

    public function img_uploads()
    {
        if (empty($_FILES)) {

            $this->error('请先上传图片');

        }

        $files = request()->file('file-zh');

        $num = 0;

        foreach ($files as $file) {

            $info = $file->move(ROOT_PATH . 'data' . DS . 'upload/shop/img/');

            if ($info) {

                $drivePath = $info->getSaveName();

            } else {

                $drivePath = '';

            }

            if ($drivePath) {

                $thumbUrl = config('view_replace_str.__UPLOAD__') . '/' . 'shop/img/' . strtr($drivePath, "\\", "/");

                add_img_db($thumbUrl);

                $img[] = $thumbUrl;

            } else {

                $num++;

                $map = array(
                    'code' => 0,
                    'esg' => '第' . $num . '上传失败！'
                );

                return $map;

            }

        }

        $map = array(
            'code' => '1',
            'img_url' => $img,
            'esg' => '上传成功！'
        );

        return $map;

    }


    public function video_upload()
    {

        $uid = Cookie::get('user_info')['id'];

        $files = request()->file('file-zh');

        if (empty($_FILES)) {

            $this->error('请先上传视频！');

        }

        $num = '0';

        foreach ($files as $file) {

            $info = $file->move(ROOT_PATH . 'data' . DS . 'upload/shop/video/');

            if ($info) {

                $drivePath = $info->getSaveName();

            } else {

                $drivePath = '';

            }

            if ($drivePath) {

                $thumbUrl = config('view_replace_str.__UPLOAD__') . '/' . 'shop/video/' . strtr($drivePath, "\\", "/");

                add_img_db($thumbUrl);

                $video_url[] = $thumbUrl;

            } else {

                $num++;

                $map = array(
                    'code' => 0,
                    'esg' => '第' . $num . '上传失败！'
                );

                return $map;
            }

            $map = array(

                'code' => '1',

                'img_url' => $video_url,

                'esg' => '上传成功！'
            );

            return $map;

        }

    }

    //删除单文件
    function del_sigle_file()
    {

        del_img_db(input('file_path'));

        $path = '.' . input('file_path');

        array_map("unlink", glob($path));

        if (!count(glob($path))) {

            $this->success(config('view_replace_str.__UPLOAD__') . '/admin/common/upload.svg');

        } else {

            $this->error('文件删除失败！');

        }

    }

}